import requests
import time
import json
# 1 1#主机非电气量
# 2 2#主机非电气量
# 3 3#主机非电气量
# 4 4#主机非电气量
# 5 电气量
# 6 电气设备记录
# 7 变频机组电气量
# 8 空
# 9 空
# 10 扬压力
# 月报
# http://192.168.100.10:8081/LLJBBXT/averageyueServlet
# 参数：nianyue: 2024-03
# http://192.168.100.10:8081/LLJBBXT/averagenianServlet 
# 参数：nian: 2024
# http://192.168.100.10:8081/LLJBBXT/LLJHISServlet
# 参数：
# startTime: 2024-03-22 00:00:00
# endTime: 2024-03-22 23:59:59
# STCD: 1
# 0:00	20.6	25.2	21.3	21.1	21.2	19.7	21.2	19.6	0.2	0.8	1.0	1.0	7.0	8.0	1.0	1.0	1.0	0.0	0.0	0.0	18.3	0.0	18.65	16.02	16.07	2.58	0.00	0.00	-3.9	0.0	0.00

def get_data(STCD):
    '''
    获取数据库数据
    STCD: 10为测压管
    返回json
    '''
    data = {
        "startTime": f"{time.strftime('%Y-%m-%d %H:00:00',time.localtime())}",
        "endTime": f"{time.strftime('%Y-%m-%d %H:59:59',time.localtime())}",
        "STCD": STCD
    }
    try:
        resp = requests.post("http://192.168.100.10:8081/LLJBBXT/LLJHISServlet",data=data)
        # print(resp.json())
        return resp.json()
    except Exception as e:
        return str(e)

def get_web():
    '''
    服务器取值
    '''
    url = 'http://192.168.100.10:8081/LLJBZ/work/realData/Alarm_Set/findAll'
    # url1 = 'http://192.168.100.10:8081/LLJBZ/work/bzRealData/bzYxReal/findAll'
    # url2 = 'http://192.168.100.10:8081/LLJBZ/work/bzRealData/bzYcReal/findAll'
    # cookies = 'Cookie: JSESSIONID=E47F56C1EFBABDEE930A68D6C95D08F3; token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlIjoi566h55CG5ZGYIiwidXNlcklkIjoiZWNhOTY1MjktODBkOS00ZDJhLTk3YWQtMTkyYzQ1M2MwZjczIiwic3ViIjoiYWRtaW4iLCJpc3MiOiIwOThmNmJjZDQ2MjFkMzczY2FkZTRlODMyNjI3YjRmNiIsImlhdCI6MTcxNjc3ODI0NCwiYXVkIjoicmVzdGFwaXVzZXIiLCJleHAiOjE3MTY3OTI2NDQsIm5iZiI6MTcxNjc3ODI0NH0.JhbgjByLa9QdbqH_Ar3Zh5NfA74XKlO8BI6tpcleni4; userName=admin; authLevel=ç®¡ç†å‘˜; Hm_lvt_82116c626a8d504a5c0675073362ef6f=1711592231,1712735867,1713320325'
    headers = {"Cookie":"JSESSIONID=E47F56C1EFBABDEE930A68D6C95D08F3; token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlIjoi566h55CG5ZGYIiwidXNlcklkIjoiZWNhOTY1MjktODBkOS00ZDJhLTk3YWQtMTkyYzQ1M2MwZjczIiwic3ViIjoiYWRtaW4iLCJpc3MiOiIwOThmNmJjZDQ2MjFkMzczY2FkZTRlODMyNjI3YjRmNiIsImlhdCI6MTcxNjc3ODI0NCwiYXVkIjoicmVzdGFwaXVzZXIiLCJleHAiOjE3MTY3OTI2NDQsIm5iZiI6MTcxNjc3ODI0NH0.JhbgjByLa9QdbqH_Ar3Zh5NfA74XKlO8BI6tpcleni4; userName=admin; authLevel=0; Hm_lvt_82116c626a8d504a5c0675073362ef6f=1711592231,1712735867,1713320325"}
    resp = requests.post(url=url,headers=headers)
    print(resp.json())
    # resp = requests.post(url=url1,headers=headers)
    # print(resp.json())
    # resp = requests.post(url=url2,headers=headers)
    # print(resp.json())
    with open('./tst.json','w') as f:
        json.dump(resp.json(),f)



def tosendcontent(resp_text):
    '''
    转换成发送内容
    '''
    result = []
    for r in resp_text:
        result.append( f"{r['tagID']}={r['value']}")
    result = f"({'&'.join(result)})"
    print(result)
    return result
        
if __name__ == "__main__":
    # print(time.strftime('%Y-%m-%d %H:00:00',time.localtime()))
    # tosendcontent(get_data(10))
    get_web()
    # while 1:
    #     now = int(time.time())
    #     if (now - 5) % 3600 == 0:
    #         get_cyg(10)
    #         # time.sleep(1)
    #     time.sleep(1)